package com.qin.malldiscount.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.malldiscount.domain.SmsFlashPromotion;
import com.qin.malldiscount.service.SmsFlashPromotionService;
import com.qin.malldiscount.mapper.SmsFlashPromotionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 覃剑
* @description 针对表【sms_flash_promotion(限时购表)】的数据库操作Service实现
* @createDate 2022-11-23 19:34:44
*/
@Service
public class SmsFlashPromotionServiceImpl extends ServiceImpl<SmsFlashPromotionMapper, SmsFlashPromotion>
    implements SmsFlashPromotionService{

    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;

    /**
     * 添加活动
     */
    @Override
    public int create(SmsFlashPromotion flashPromotion) {
        flashPromotion.setCreateTime(new Date());
        int count = flashPromotionMapper.insert(flashPromotion);
        return count;
    }

    /**
     * 修改指定活动
     */
    @Override
    public int update(Long id, SmsFlashPromotion flashPromotion) {
        flashPromotion.setId(id);
        int count = flashPromotionMapper.updateById(flashPromotion);
        return count;
    }

    /**
     * 删除单个活动
     */
    @Override
    public int delete(Long id) {
        int count = flashPromotionMapper.deleteById(id);
        return count;
    }

    /**
     * 修改上下线状态
     */
    @Override
    public int updateStatus(Long id, Integer status) {
        SmsFlashPromotion flashPromotion = new SmsFlashPromotion();
        flashPromotion.setStatus(status);
        flashPromotion.setId(id);
//        QueryWrapper<SmsFlashPromotion> wrapper = new QueryWrapper<>();
//        wrapper.eq("id",id);
//        int count = flashPromotionMapper.update(flashPromotion, wrapper);
        int count = flashPromotionMapper.updateById(flashPromotion);
        return count;
    }

    /**
     * 获取详细信息
     */
    @Override
    public SmsFlashPromotion getItem(Long id) {
        SmsFlashPromotion flashPromotion = flashPromotionMapper.selectById(id);
        return flashPromotion;
    }

    /**
     * 分页查询活动
     */
    @Override
    public Page<SmsFlashPromotion> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<SmsFlashPromotion> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsFlashPromotion> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like("title",keyword);
        }
        Page<SmsFlashPromotion> selectPage = flashPromotionMapper.selectPage(page, wrapper);
        
        return selectPage;
    }
}




